import EXIF from 'exif-js'
 
export default {
    getOrientation: (file) => {
        return new Promise((resolve) => {
            EXIF.getData(file, function () {
                const orient = EXIF.getTag(this, 'Orientation')
                resolve(orient)
            })
        })
    },
  
    dataURLtoFile: (dataurl, filename) => {
        const arr = dataurl.split(',')
        const mime = arr[0].match(/:(.*?);/)[1]
        const bstr = atob(arr[1])
        let n = bstr.length
        let u8arr = new Uint8Array(n);
        while (n--) {
            u8arr[n] = bstr.charCodeAt(n);
        }
        return new File([u8arr], filename, {type: mime});
    },
  
    rotateImage: (image, width, height,orient) => {
        
        let canvas = document.createElement('canvas')
        let ctx = canvas.getContext('2d')
        ctx.save()
        canvas.width = width;
        canvas.height = height;
        // canvas.width = height
        // canvas.height = width
        // if ([5, 6, 7, 8].includes(orient)) {
        //     canvas.width = height;
        //     canvas.height = width;
        //   }
        //Math.PI 是180
        switch(orient){
              // case 2:
              //   ctx.transform(-1, 0, 0, 1, width, 0);
              //   break;
              // case 3:
              //   ctx.transform(-1, 0, 0, -1, width, height);
              //   break;
              // case 4:
              //   ctx.transform(1, 0, 0, -1, 0, height);
              //   break;
              // case 5:
              //   ctx.transform(0, 1, 1, 0, 0, 0);
              //   break;
              // case 6:
              //   ctx.transform(0, 1, -1, 0, height, 0);
              //   break;
              // case 7:
              //   ctx.transform(0, -1, -1, 0, height, width);
              //   break;
              // case 8:
              //   ctx.transform(0, -1, 1, 0, 0, width);
              //   break;
              default:
                ctx.transform(1, 0, 0, 1, 0, 0);
        }
        ctx.drawImage(image, 0, 0, width, height);
        ctx.restore()
        return canvas.toDataURL("image/jpeg")
    },
}